<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$merchid=$this->checklogin();
	set_html('title','在线支付');
	$return['merchid']=$merchid;
	if(!$merchid){
		exi('参数错误','error');
	}
	$merch=pdo_get('jy_merch_user',array('id'=>$merchid));
	$paycode=$_RQ['paycode'];
	//$paycode='558531579142';
	if(!$paycode){
		exi('请扫客户收款码！','error');
	}
	$member=pdo_get('core_members',array('paycode'=>$paycode,'uniacid'=>$_SESSION['uniacid']),array('id','paycode','paycodetime','nickname','realname','username','mobile','avatar','credit1','credit2','credit3','credit4','credit5','credit6'));
	if($member['paycodetime']<time()-300){
		$member['ispass']=1;
	}
	$return['merch']=$merch;
	$return['member']=$member;
	exi($return);
}
if($_RQ['op']=='post'){
		$pay_log=get_postdata();
		$merchid=$pay_log['merchid'];
		$mid=$pay_log['mid'];
		if($pay_log['fee']<=0){
			exi('请输入支付金额！','error');
		}
		$di_open=$pay_log['di_open'];
		$di_fee=$pay_log['di_fee']?:0;
		$lastfee=$pay_log['fee']-$di_fee;
		$credit2fee=pdo_getcolumn('core_members',array('id'=>$mid),'credit2');
		if($lastfee>$credit2fee){
			exi('不足！当前账户[余额]：'.$credit2fee,'error');
		}
		//如果是余额支付，就不抵扣
		if($di_open && $di_open!='credit2' && $di_fee>0){
			$creditnum=pdo_getcolumn('core_members',array('id'=>$mid),$di_open);
			$key=substr($di_open, -1);
			$typetitle=credit_types()[$key];
			$credit_rate=$setting['credit'.$key.'_rate']?:1;
			if($di_fee>$creditnum){
				exi($typetitle.'不足！当前账户剩余：'.$creditnum,'error');
			}
		}
		if($lastfee>0){
			model('core/member')->member_credit($mid,'credit2',$lastfee,false,'jy_merch','付款码付款');
		}
		if($di_fee>0 && $key){
			model('core/member')->member_credit($mid,'credit'.$key,$di_fee,false,'jy_merch','付款码付款');
		}
		$data=array(
			'ordersn'=>'SN'.get_ordersn('jy_merch_paylog','ordersn'),
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'merchid'=>$merchid,
			'money'=>$postdata['money'],
			'status'=>1,
			'paytime'=>time(),
			'pay_type'=>'credit',
			'createtime'=>time(),
		);
		pdo_insert('jy_merch_paylog',$data); 
		$data['id']=pdo_insertid();
		$data['paytype']=$data['pay_type'];
		$data['price']=$data['money'];
		$this->model('order')->buy_credit3($data,true);
		if($this->zhaoshang['is_downfee']){
			$data['codepay']=1;
			$this->model('commission')->get_zhaoshangorder($data);
			$this->model('commission')->order_complete($data);
		}
		if($this->plugin_exists('jy_paimerch')){
			$jy_paimerch=plugin('jy_paimerch')->plugin_setting('basic');
			if($jy_paimerch['isopen']){
				$data['tablename']='jy_merch_paylog';
				model('jy_paimerch/pai')->create($data);
			}
		}
		if($this->plugin_exists('jy_newpaimerch')){
			$jy_newpaimerch=plugin('jy_newpaimerch')->plugin_setting('basic');
			if($jy_newpaimerch['isopen']){
				$data['tablename']='jy_merch_paylog';
				model('jy_newpaimerch/pai')->create($data,1);
			}
		}
		exi('收款成功','','/manage.index/');
}
if($_RQ['op']=='getpay'){
	$pay=$_RQ;
    $pay_types=array('credit');
    foreach($adapay_pay_types as $key=>$type){
          $return['is'.$type]=1;
     }
    $return['pay_types']=array_values($pay_types);
	$return['mid']=$_SESSION['uid'];
    if($pay['merchid']){
        $member=pdo_get('jy_merch_member',array('merchid'=>$pay['merchid'],'mid'=>$pay['mid']),array('id','merchid','credit2','credit3','credit4','credit5','credit6'));
    }
    if(!$member['merchid']){
        $member=pdo_get('core_members',array('id'=>$pay['mid']),array('credit2','credit3','credit4','credit5','credit6'));
    }
    $return['credit']=$member['credit2'];
    if(!$pay['merchid']){
         $creditdata=model('core/member')->creditdata($member);
    }else{
         $creditdata=model('jy_merch/member')->creditdata($member);
    }
    foreach($creditdata as $k=>&$cv){
            //剔除余额支付
            if($cv['type']==2){
                unset($creditdata[$k]);
            }
            if($pay['no_type']){
                foreach($pay['no_type'] as $nov){
                    if($nov==$cv['field']){
                        unset($creditdata[$k]);
                        continue;
                    }
                }
            }
            $cv['value']=$member['credit'.$cv['type']];
            if($cv['value']<=0){
                $cv['difee']=0.00;
                continue;
            }
            $cv['dirate']=$cv['dirate']?:100;
            $cv['rate']=$cv['rate']?:1;
            $cv['difee']=$pay['fee']*$cv['dirate']/100;
            $needfee=$cv['rate']*$cv['difee'];
            if($cv['difee']>0){
                if($needfee>$cv['value']){
                    $cv['difee']=$cv['value']/$cv['rate'];
                }
                $cv['difee']=round($cv['difee'],2);
            }
    }
    $return['creditdata']=$creditdata;
    if($pay['fee']<=0){
        $return['type']='credit';
    }
    //余额抵扣
    if($pay['fee']>0 && $return['credit']>0 && $pay['fee']>$return['credit']){
        $difee=$pay['fee']>$return['credit']?$return['credit']:$pay['fee'];
        $return['credit2']=array('isdi'=>1,'difee'=>$difee);
    }
	$return['fee']=price($pay['fee']);
    $return['di_open']=0;
    exi($return);
}
